package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Productfault;
import cn.xpg.service.ProductfaultService;
import cn.xpg.tools.PageSupport;

@Controller
public class ProductfaultController {

	@Resource(name = "productfaultService")
	private ProductfaultService productfaultService;

	// 分页查询产品故障
	@RequestMapping("/getProductfaultList.do")
	public String getProductfaultList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String productfault_name = request.getParameter("productfault_name");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = productfaultService.getProductfaultCount(productfault_name);
		System.out.println("查询产品故障信息！！！！！！！！！！————>getProductfaultList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Productfault> productfaultList = null;
		productfaultList = productfaultService.getProductfaultList(productfault_name, (currentPageNo - 1) * pageSize,
				pageSize);

		request.setAttribute("productfaultList", productfaultList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("productfault_name", productfault_name);
			return "/jsp/base/productfault_list";
	}

	// 新增产品故障
	@RequestMapping("addProductfault.do")
	public String addProductfault(@Valid Productfault productfault) {
		System.out.println("新增产品故障信息！！！！！！！！！！————>addProductfault");
		if (productfaultService.addProductfault(productfault) > 0) {
			return "redirect:/getProductfaultList.do";
		} else {
			return "/jsp/base/productfault_list";
		}
	}

	// 获取产品故障Id
	@RequestMapping("getProductfaultId.do")
	public String getProductfaultId(HttpServletRequest request) {
		System.out.println("获取产品故障Id！！！！！！！！！！————>getProductfaultId");
		int productfault_id = Integer.parseInt(request.getParameter("productfault_id"));
		Productfault productfault = productfaultService.getProductfaultId(productfault_id);
		request.setAttribute("productfault", productfault);
		return "/jsp/base/productfault_update";
	}

	// 修改产品故障
	@RequestMapping("updateProductfault.do")
	public String updateProductfault(@Valid Productfault productfault, HttpServletRequest request) {
		System.out.println("修改产品故障信息！！！！！！！！！！————>updateProductfault");
		if (productfaultService.updateProductfault(productfault) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getProductfaultList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("productfault", productfault);
			return "/jsp/base/productfault_update";
		}
	}

	// 删除产品故障
	@RequestMapping("deleteProductfault.do")
	public String deleteProductfault(HttpServletRequest request) {
		System.out.println("删除产品故障信息！！！！！！！！！！————>deleteProductfault");
		int productfault_id = Integer.valueOf(request.getParameter("productfault_id"));
		if (productfaultService.deleteProductfault(productfault_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getProductfaultList.do";
	}
}
